#include <QListWidgetItem>
#include <QFileDialog>
#include <QDebug>

#include "rentgendelegate.h"
#include "rentgenwidget.h"

#include "ui_rentgenwidget.h"
#include "manager.h"
#include "defs.h"

RentgenWidget::RentgenWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RentgenWidget)
{
    ui->setupUi(this);
    ui->lstRentgens->setIconSize(QSize(80, 80));
}

RentgenWidget::~RentgenWidget()
{
    delete ui;
}

void RentgenWidget::setup()
{
    ui->lstRentgens->setItemDelegate(new RentgenDelegate(this));

    connect(ui->btnAdd, SIGNAL(clicked()),
            App->mainWindow(), SLOT(markAsModified()));
    connect(ui->btnDelete, SIGNAL(clicked()),
            App->mainWindow(), SLOT(markAsModified()));

    ui->btnDelete->setEnabled(false);
    ui->btnView->setEnabled(false);

    setupModel();
}

void RentgenWidget::setupModel()
{
    if (App->pacient()->rentgens()==NULL)
    {
        LOG(objectName(), "App->pacient()->rentgens()==NULL");
    } else {
        ui->lstRentgens->setModel( App->pacient()->rentgens() );
    }
}

void RentgenWidget::clearAll()
{
    ui->btnDelete->setEnabled(false);
}

void RentgenWidget::setButtonsEnabled(bool enabled)
{
    ui->btnDelete->setEnabled(enabled);
    ui->btnView->setEnabled(enabled);
}

void RentgenWidget::addLine(const QString &title, const QString &src, const QString &ppc, const QString &comment)
{
    QVariantList vl;
    vl << title << src << ppc << comment;
    App->pacient()->addRentgen(vl);
}

void RentgenWidget::on_btnAdd_clicked()
{
    QString imgPath = QFileDialog::getOpenFileName(this,
                                 UkrString("Відкрити фото зображення рентгенограми"),
                                 "",
                                 InOutPicturesTypes);
    if (imgPath.isEmpty()) { return; }

    imgPath = QDir::toNativeSeparators(imgPath);
    imgPath = App->copyImage( imgPath );
    imgPath = App->fileInCurrent(imgPath);
    if (imgPath.isEmpty()) { return; }

    QFileInfo fi(imgPath);
    addLine( "", fi.fileName(), "0", "" );

    ui->btnDelete->setEnabled(true);
    ui->btnView->setEnabled(true);
    App->mainWindow()->markAsModified();
}

void RentgenWidget::on_btnDelete_clicked()
{
    const int curRow = ui->lstRentgens->currentIndex().row();

    if (curRow<0) return;

    QString iconName(App->pacient()->rentgens()->data(
                         App->pacient()->rentgens()->index(curRow, RentSrc)).toString());

    App->pacient()->rentgens()->removeRow(curRow);
    App->freeIcon(iconName);

    if (App->pacient()->rentgensCount()==0)
    {
        ui->btnDelete->setEnabled(false);
        ui->btnView->setEnabled(false);
        App->mainWindow()->markAsModified();
    }
}

void RentgenWidget::on_btnView_clicked()
{
    const int curRow = ui->lstRentgens->currentIndex().row();
    if ( App->pacient()->rentgensCount()!=0
         && curRow>=0 )
    {
        App->rentgenViewForm()->setup(App->pacient()->fullName(), curRow);
        App->rentgenViewForm()->show();
    }
}
